package sk.stuba.fiit.ltasys.web.mvc.model.entity;

import java.io.IOException;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.bouncycastle.tsp.TimeStampResponse;
import org.bouncycastle.tsp.TimeStampToken;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import com.sun.xml.internal.ws.developer.MemberSubmissionAddressing;

/**
 * Timestamp Entity.
 * 
 * @author Marek Hruska
 */

@Entity
@Table(name = "TIMESTAMP")
public class TimestampEntity implements Comparable<TimestampEntity>{
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	private Date genTimestamp;
	private byte[] timestamp;
	private Date validFrom;
	private Date validTil;
	private String issuer;
	private boolean leaf;
	@OneToOne()
	@LazyToOne(LazyToOneOption.FALSE)
	private ArchivePackageEntity archiveRecord;
	
	public TimestampEntity() {	}

	
	public void convertTimeStampTokenToEntity(TimeStampResponse resp, ArchivePackageEntity record,CertificateEntity certificate) throws IOException{
		this.genTimestamp = new Date();
		this.archiveRecord = record;
		this.issuer = certificate.getIssuer();
		this.leaf = true;
		this.timestamp = resp.getEncoded(); 
		this.validFrom = certificate.getValid_from();
		this.validTil = certificate.getValid_to();
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getGenTimestamp() {
		return genTimestamp;
	}

	public void setGenTimestamp(Date genTimestamp) {
		this.genTimestamp = genTimestamp;
	}

	public byte[] getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(byte[] timestamp) {
		this.timestamp = timestamp;
	}

	public Date getValidFrom() {
		return validFrom;
	}

	public void setValidFrom(Date validFrom) {
		this.validFrom = validFrom;
	}

	public Date getValidTil() {
		return validTil;
	}

	public void setValidTil(Date validTil) {
		this.validTil = validTil;
	}

	public String getIssuer() {
		return issuer;
	}

	public void setIssuer(String issuer) {
		this.issuer = issuer;
	}

	public int compareTo(TimestampEntity o) {
		if(o!= null){
			if(o.getGenTimestamp().before(this.getGenTimestamp()))
				return -1;
			else {
				return 1;
			}
		}
		return 1;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public ArchivePackageEntity getArchiveRecord() {
		return archiveRecord;
	}

	public void setArchiveRecord(ArchivePackageEntity archiveRecord) {
		this.archiveRecord = archiveRecord;
	}
	
	
	
}
